<?php
namespace app\walletyhbbadmin\controller;
use Think\Db;
use Think\request;
require_once (APP_PATH .'api/FinanceApi.php');
class Share extends AdminBase {
    public function _initialize(){
        parent::_initialize();
    }

    //首页
    public function index(){
        $list = Db::name('share_config')->select();
        foreach($list as $k=>$v){
            if($v['type']==1){
                $list[$k]['type_name'] = '赠送礼品';
            }
            if($v['type']==2){
                $list[$k]['type_name'] = '商品分享';
            }
            if($v['type']==3){
                $list[$k]['type_name'] = '夺宝分享';
            }
            if($v['type']==4){
                $list[$k]['type_name'] = '代理分享';
            }
        }
        $this->assign('list',$list);
        return $this->fetch();
    }


    /*
     * 分享信息修改
     */
    public function shareUpdate(){
        $id = input('id');
        $info = Db::name('share_config')->where('id',$id)->find();
        if($info['type']==1){
            $info['type_name'] = '赠送礼品';
        }
        if($info['type']==2){
            $info['type_name'] = '商品分享';
        }
        if($info['type']==3){
            $info['type_name'] = '夺宝分享';
        }
        if($info['type']==4){
            $info['type_name'] = '代理分享';
        }
        $this->assign('info',$info);
        return $this->fetch('add');
    }

    /*
     * 分享修改提交
     */
    public function shareUpdateSub(){
        $id = input('id');
        if(trim(input('title'))==''){
            $this->error('请填写分享标题',U('Share/shareUpdate',array('id',$id)));
        }
        $data['title'] = trim(input('title'));
        $file = request()->file('img');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'share');
            //存储图片上传的路径
            $data['img'] = '/public/uploads/share/' . str_replace('\\', '/', $info->getsaveName());
        }
        $res = Db::name('share_config')->where('id',$id)->update($data);
        if($res!==false){
            $this->success('修改成功',U('Share/index'));
        }else{
            $this->error('系统错误');
        }
    }




}